package com.gviiii.baiduai.ai;

import cn.hutool.core.io.FileUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.gviiii.baiduai.auth.AuthClient;
import com.gviiii.baiduai.config.ConfigRegistry;
import com.gviiii.baiduai.config.ServiceConfig;
import com.gviiii.baiduai.file.ImageFile;
import com.gviiii.baiduai.pojo.FileInfo;
import com.gviiii.baiduai.pojo.ImageResultInfo;
import com.gviiii.baiduai.util.HttpClientUtil;
import com.gviiii.baiduai.util.JsonUtil;

import java.io.File;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Map;

public abstract class AbstractAiService implements Aiservice {

    private static final Log log = LogFactory.get();

    protected ServiceConfig serviceConfig;

    private FileInfo fileInfo;

    private ConfigRegistry configRegistry;

    public AbstractAiService() {

    }

    public AbstractAiService(ServiceConfig serviceConfig, FileInfo fileInfo, ConfigRegistry configRegistry) {
        this.serviceConfig = serviceConfig;
        this.fileInfo = fileInfo;
        this.configRegistry = configRegistry;
    }

    /**
     * 识别方法
     * @param paraMap
     * @return
     */
    @Override
    public AbstractAiService distinguish(Map paraMap) {
        String result = HttpClientUtil.HttpJsonPostRequest(setApi(), JsonUtil.witeStringByObject(paraMap));
        ImageResultInfo imageResultInfo = (ImageResultInfo) JsonUtil.readObjectByString(result, ImageResultInfo.class);
        resolverResult(imageResultInfo);
        return this;
    }

    /**
     * 输出识别结果
     * @param imageResultInfo
     * @return
     */
    @Override
    public AbstractAiService resolverResult(ImageResultInfo imageResultInfo) {
        if (imageResultInfo != null && imageResultInfo.getResults().size() > 0) {
            for (int i = 0; i < imageResultInfo.getResults().size(); i++) {
                switchImgsDir(serviceConfig, fileInfo, imageResultInfo.getResults().get(i), i);
            }
        }
        return this;
    }

    /**
     * @param fileInfo
     * @param api
     * @param threshold
     */
    protected abstract void distinguish(FileInfo fileInfo, String api, double threshold);

    /**
     * @param fileInfo
     */
    public abstract void distinguish(FileInfo fileInfo);

    /**
     * @param fileInfo
     * @param resultsBean
     * @param i
     */
    protected abstract void switchImgsDir(ServiceConfig serviceConfig, FileInfo fileInfo, ImageResultInfo.ResultsBean resultsBean, int i);

    /**
     * 设置模型调用接口
     * @return
     */
    @SuppressWarnings("unchecked")
    private String setApi() {
        return serviceConfig.getApi() + "?access_token=" + AuthClient.aipEasydlClient(configRegistry).get("accesstoken");
    }

    /**
     * 生成满足条件的图片
     * @param fileInfo
     * @param resultsBean
     * @param i
     */
    protected void createImageFile(Map<String,String> resultMap, FileInfo fileInfo,ImageResultInfo.ResultsBean resultsBean, int i) {
        ImageFile.cutImage(Paths.get(fileInfo.getFilePath()).toFile(), createFile(fileInfo, i, resultMap.get(resultsBean.getName())), resultsBean.getLocation());
    }

    /**
     * 创建文件
     * @param file
     * @param count
     * @param path
     * @return
     */
    private static File createFile(FileInfo file, int count, String path) {
        StringBuffer sb = new StringBuffer();
        sb.append(path).append(file.getFileName()).append("-").append(count).append(".").append(file.getFileExName());
        return Paths.get(sb.toString()).toFile();
    }

    /**
     * 复制文件
     * @param resultMap
     * @param fileInfo
     * @param resultsBean
     */
    protected void copyImageFile(Map<String,String> resultMap, FileInfo fileInfo, ImageResultInfo.ResultsBean resultsBean) {
        FileUtil.copyFile(Paths.get(fileInfo.getFilePath()).toFile(), Paths.get(new StringBuilder().append(resultMap.get(resultsBean.getName())).append(fileInfo.getFileName()).append(fileInfo.getFileExName()).toString()).toFile(), StandardCopyOption.REPLACE_EXISTING);
    }
}
